package com.steven.ourcontest.report.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.google.android.flexbox.FlexboxLayout
import com.google.gson.JsonArray
import com.google.gson.JsonParser
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.chat.bean.Report
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.utils.DensityUtil
import com.steven.ourcontest.utils.ScreenUtils
import com.steven.ourcontest.widget.EnlargeImageDialog
import kotlinx.android.synthetic.main.activity_forum_discuss.*

class ReportAdapter(private val context: Context, private val handleClickListener: (eventId:Long)->Unit) :
    RecyclerView.Adapter<ViewHolder>() {
    val listData by lazy {
        ArrayList<Report>()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater =
            LayoutInflater.from(context).inflate(R.layout.item_handle_report, parent, false)
        return ViewHolder(inflater)
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemData = listData[position]
        holder.apply {
            tv_id?.text = "投诉编号：${itemData.id}"
            tv_uid?.text = "被投诉账号ID：${itemData.uid}"
            tv_find_user?.setOnClickListener {
                AppRouter.openUserDetailActivity(context, itemData.uid)
            }
            tv_content?.text = "内容：${itemData.content}"
            if (itemData.images?.isNotEmpty()==true){
                itemData.images.let { images ->
                    val parser = JsonParser()
                    val jsonArray: JsonArray = parser.parse(images).asJsonArray
                    jsonArray.forEach { JsonElement ->
                        val imageView = ImageView(context)
                        val layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                        )
                        DensityUtil.dp2px(context, 24f).let { dp ->
                            layoutParams.width = (ScreenUtils.getScreenWidth(context) - dp) / 3 - dp / 3
                            layoutParams.height = layoutParams.width
                            layoutParams.marginStart = dp / 6
                            layoutParams.marginEnd = dp / 6
                            layoutParams.topMargin = dp / 6
                            layoutParams.bottomMargin = dp / 6
                        }
                        imageView.layoutParams = layoutParams
                        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
                        flex_container?.addView(imageView)
                        imageView.setOnClickListener {
                            EnlargeImageDialog(context).setUrl(JsonElement.asString).show()
                        }
                        Glide.with(context)
                            .load(NetConstants.ICON_BASE_URL + JsonElement.asString)
                            .placeholder(R.drawable.ic_placeholder)
                            .dontAnimate()
                            .error(R.drawable.aio_image_fail)
                            .into(imageView)
                    }
                }
                tv_update_result?.setOnClickListener{
                    handleClickListener.invoke(itemData.id)
                }
            }
        }
    }
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val tv_id = itemView.findViewById<TextView>(R.id.tv_id)
    val tv_uid = itemView.findViewById<TextView>(R.id.tv_uid)
    val tv_find_user = itemView.findViewById<TextView>(R.id.tv_find_user)
    val tv_content = itemView.findViewById<TextView>(R.id.tv_content)
    val flex_container = itemView.findViewById<FlexboxLayout>(R.id.flex_container)
    val tv_update_result = itemView.findViewById<TextView>(R.id.tv_update_result)
}